import axios from 'axios'
import { idText } from 'typescript'
import { getTokenInfo } from './storage'

// 请求基地址
export const baseURL = 'http://geek.itheima.net'

const http = axios.create({
  baseURL: baseURL,
  timeout: 5000,
})

// 请求拦截器
http.interceptors.request.use(
  function (config: any) {
    const { token } = getTokenInfo()
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

// 响应拦截器
http.interceptors.response.use(
  function (response) {
    return response.data
  },
  function (error) {
    return Promise.reject(error)
  }
)

export default http
